package com.fly.glide;

import android.os.Environment;
import android.util.Log;

import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;

import java.io.File;

/**
 * Created with Android Studio.
 * Package_Name：com.fly.glide
 * Project_Name：GlideDemo
 * User：郭鹏飞
 * Date：2015/12/7
 * Email：love518420@foxmail.com
 * Description：在sd卡缓存 与Glide提供的区别
 * Glide 目录 /storage/emulated/0/Android/data/package-name/
 * 自定义 目录 /storage/emulated/0/package-name/
 * 功能区别 Glide默认路径里面的数据随着应用的卸载会被删除
 *          自定义的路径里面的数据不会随着应用的卸载被删除
 */
public class CustomExternalCacheDiskCacheFactory extends DiskLruCacheFactory {
    public CustomExternalCacheDiskCacheFactory() {
        this(DiskCache.Factory.DEFAULT_DISK_CACHE_DIR, DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE);
    }

    public CustomExternalCacheDiskCacheFactory(int diskCacheSize) {
        this(DiskCache.Factory.DEFAULT_DISK_CACHE_DIR, diskCacheSize);
    }

    public CustomExternalCacheDiskCacheFactory(final String diskCacheName, int diskCacheSize) {
        super(new CacheDirectoryGetter() {
            @Override
            public File getCacheDirectory() {
                File cacheDirectory = Environment.getExternalStorageDirectory();
                if (cacheDirectory == null) {
                    return null;
                }
                if (diskCacheName != null) {
                    return new File(cacheDirectory, diskCacheName);
                }
                Log.e("tag", cacheDirectory.getAbsolutePath());
                return cacheDirectory;
            }
        }, diskCacheSize);
    }
}
